package com.huterox.linuxrember.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;


import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.huterox.linuxrember.Dao.ReviewDBEngine;
import com.huterox.linuxrember.R;

import com.huterox.linuxrember.Dao.UserDBEngine;
import com.huterox.linuxrember.utils.AssignNeedReview;

public class ReviewActivity extends AppCompatActivity {

    private TextView tv_command;
    //    使用案例
    private TextView tv_usage;
    private Button bt_grammar;
    private Button bt_option;
    private Button bt_example;
    private Button bt_next;
    private Button bt_break;

    private UserDBEngine userDBEngine;
    private ReviewDBEngine reviewDBEngine;

    /*
    * 需要复习的命令的index，不同阶段的意思是不同章节
    * */
    static String NeedReviewComm[] = new String[500];

    //跳转到主界面
    public void MainAction(View view, int id) {
        Intent intent = new Intent(this, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    //跳转到语法界面
    private void GrammarAction(View view, int id, String command, int flag) {

        Intent intent = new Intent(this, GrammarActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putString("command", command);
        bundle.putInt("flag", flag);
        intent.putExtras(bundle);
        startActivity(intent);

    }

    //跳转到选项界面
    private void OptionAction(View view, int id, String command, int flag) {

        Intent intent = new Intent(this, OptionActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putString("command", command);
        bundle.putInt("flag", flag);
        intent.putExtras(bundle);
        startActivity(intent);

    }

    //跳转到实例界面，传递对应信息，方便实例化
    private void ExampleAction(View view, int id, String command, int flag) {

        Intent intent = new Intent(this, ExampleActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putString("command", command);
        bundle.putInt("flag", flag);
        intent.putExtras(bundle);
        startActivity(intent);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review);

        userDBEngine = new UserDBEngine(this);
        reviewDBEngine = new ReviewDBEngine(this);

        Intent intent = getIntent();
        int id = intent.getExtras().getInt("id");
        int level = userDBEngine.querylevelByid(getWindow().getDecorView(), id);
        AssignNeedReview.Assign(getWindow().getDecorView(), NeedReviewComm, level);
        initView();
    }

    //初始化
    private void initView() {

        tv_command = findViewById(R.id.tv_command);
        tv_usage = findViewById(R.id.tv_usage);
        bt_grammar = findViewById(R.id.bt_grammar);
        bt_option = findViewById(R.id.bt_option);
        bt_example = findViewById(R.id.bt_example);
        bt_next = findViewById(R.id.bt_next);
        bt_break = findViewById(R.id.bt_break);

        //设置监听
        ReviewActivity.MyOnClickListener listener = new ReviewActivity.MyOnClickListener();
        tv_command.setOnClickListener(listener);
        tv_usage.setOnClickListener(listener);
        bt_grammar.setOnClickListener(listener);
        bt_option.setOnClickListener(listener);
        bt_example.setOnClickListener(listener);
        bt_next.setOnClickListener(listener);
        bt_break.setOnClickListener(listener);


        Intent intent = getIntent();
        int id = intent.getExtras().getInt("id");
        /*
         * 复习的顺序是按照一个章节一个章节来的，你看了多少几个章节
         * 按照顺序开始复习
         * */
        int level = userDBEngine.querylevelByid(getWindow().getDecorView(), id);
        int t = AssignNeedReview.size(level);
        int c = userDBEngine.queryRnByid(getWindow().getDecorView(), id);

        if (c >= t) {
            c = t - 1;
        }
        tv_command.setText(NeedReviewComm[c]);
        reviewDBEngine.open();
        String usage1 = reviewDBEngine.queryUsageBycommand(getWindow().getDecorView(), NeedReviewComm[c]);
        tv_usage.setText(usage1);

    }

    private class MyOnClickListener implements View.OnClickListener {

        Intent intent = getIntent();
        int id = intent.getExtras().getInt("id");


        @Override
        public void onClick(View view) {

            int level = userDBEngine.querylevelByid(view, id);
            int t = AssignNeedReview.size(level);
            int c = userDBEngine.queryRnByid(view, id);

            switch (view.getId()) {
                case R.id.bt_next:
                    c++;
                    userDBEngine.Rn(view, id, c);
                    if (c < t) {

                        tv_command.setText(NeedReviewComm[c]);
                        reviewDBEngine.open();
                        String usage1 = reviewDBEngine.queryUsageBycommand(view, NeedReviewComm[c]);
                        tv_usage.setText(usage1);

                    } else {

                        userDBEngine.nextlevel(view, id);
                        userDBEngine.Rn(view, id, 0);
                        Toast.makeText(ReviewActivity.this, "已完成本阶段的学习,自动返回主页", Toast.LENGTH_SHORT).show();
                        MainAction(view, id);
                    }
                    break;

                case R.id.bt_grammar:
                    GrammarAction(view, id, NeedReviewComm[c], 3);
                    break;
                case R.id.bt_option:
                    OptionAction(view, id, NeedReviewComm[c], 3);
                    break;
                case R.id.bt_example:
                    ExampleAction(view, id, NeedReviewComm[c], 3);
                    break;
                case R.id.bt_break:
                    MainAction(view, id);
                    break;
            }
        }
    }
    

}